package JZ_Offer.dataStructure.tree;

import base.TreeNode;

/**
 * 对称的二叉树
 * <p>
 * 给定一棵二叉树，判断其是否是自身的镜像（即：是否对称）
 */
public class JZ28_对称的二叉树 {

    private static boolean isSymmetrical(TreeNode pRoot) {
        if (pRoot == null || (pRoot.left == null && pRoot.right == null)) {
            return true;
        }
        if (pRoot.left.val != pRoot.right.val) {
            return false;
        }
        return equals(pRoot.left, pRoot.right);
    }

    private static boolean equals(TreeNode l, TreeNode r) {
        if (l == null && r == null) {
            return true;
        }
        if (l == null || r == null) {
            return false;
        }
        if (l.val != r.val) {
            return false;
        }

        return equals(l.left, r.right) && equals(l.right, r.left);
    }

}
